<?php
class ControllerOrderReturnReason extends Controller {
 
	public function index() {
		$this->load->language('order/return_reason');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('order/return_reason');
		$this->getList();
	}

	public function insert() {
		$this->load->language('order/return_reason');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('order/return_reason');
		if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
			$this->model_order_return_reason->addReturnReason($this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('order/return_reason', 'token=' . $this->session->data['token'], 'SSL'));
		}
		$this->getForm_text();
		$this->getForm_insert();
	}

	public function update() {
		$this->load->language('order/return_reason');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('order/return_reason');
		if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
			$this->model_order_return_reason->editReturnReason($this->request->get['return_reason_id'], $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('order/return_reason', 'token=' . $this->session->data['token'], 'SSL'));
		}
		$this->getForm_text();
		$this->getForm_update();
	}

	private function getList() {
  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('order/return_reason', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
							
		$this->data['insert'] = $this->url->link('order/return_reason/insert', 'token=' . $this->session->data['token'], 'SSL');
	
		$this->data['return_reasons'] = array();
		$results = $this->model_order_return_reason->getReturnReasones();
		foreach ($results as $result) {
			$action = array();
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('order/return_reason/update', 'token=' . $this->session->data['token'] . '&return_reason_id=' . $result['return_reason_id'], 'SSL')
			);		
			$this->data['return_reasons'][] = array(
				'return_reason_id' => $result['return_reason_id'],
				'name'              => $result['name'],
				'display_name'      => $result['display_name'],
				'action'            => $action
			);
		}	
	
		$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->data['text_no_results'] = $this->language->get('text_no_results');

		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_display_name'] = $this->language->get('column_display_name');
		$this->data['column_action'] = $this->language->get('column_action');

		$this->data['button_insert'] = $this->language->get('button_insert');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$this->template = 'order/return_reason_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
 	}

	private function getForm_text() {
		$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->data['entry_name'] = $this->language->get('entry_name');
		
		$this->data['entry_display_name'] = $this->language->get('entry_display_name');
		$this->data['entry_description'] = $this->language->get('entry_description');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
 		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = array();
		}

  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('order/return_reason', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
   		
		$this->data['token'] = $this->session->data['token'];
		
		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
	}

	private function getForm_insert() {
		$this->data['action'] = $this->url->link('order/return_reason/insert', 'token=' . $this->session->data['token'], 'SSL');
    	$this->data['cancel'] = $this->url->link('order/return_reason', 'token=' . $this->session->data['token'], 'SSL');

		$this->data['return_reason_description'] = array();
		
		$this->data['name'] = '';
		
		$this->template = 'order/return_reason_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render()); 
	}
	
	private function getForm_update() {
		$this->data['action'] = $this->url->link('order/return_reason/update', 'token=' . $this->session->data['token'] . '&return_reason_id=' . $this->request->get['return_reason_id'], 'SSL');
		$this->data['cancel'] = $this->url->link('order/return_reason', 'token=' . $this->session->data['token'], 'SSL');

		$this->data['return_reason_description'] = $this->model_order_return_reason->getReturnReasonDescriptions($this->request->get['return_reason_id']);
			
		$return_reason_info = $this->model_order_return_reason->getReturnReason($this->request->get['return_reason_id']);
		$this->data['name'] = $return_reason_info['name'];

		$this->template = 'order/return_reason_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render()); 
	}
}
?>